package org.example.service.impl.en;

import org.example.mapper.en.UserDepositDormitoryEnMapper;
import org.example.pojo.en.UserDepositDormitoryEn;
import org.example.service.api.en.UserDepositDormitoryEnInterface;
import org.example.util.JSONStats;
import org.example.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserDepositDormitoryEnInterfaceImpl implements UserDepositDormitoryEnInterface {
    @Autowired
    UserDepositDormitoryEnMapper userDepositDormitoryEnMapper;

    @Override
    public PageList getUserDepositDormitryAll(Integer userId, Integer page, Integer pageSize) {

        PageList userQueryResult = new PageList();
        //定义页数变量
        int totalPage;

        int start;
        //所取元素的起始位置
        if(page==1){
            start=0;
        }else {
            start = (page-1)  * pageSize;
            //所取元素的结束位置  pageSize

        }
        List<UserDepositDormitoryEn> userDepositDormitoryEnList =userDepositDormitoryEnMapper.getUserDepositDormitryAll(userId, start, pageSize);
        System.out.println(userDepositDormitoryEnList);

        if (userDepositDormitoryEnList != null) {
            if(userDepositDormitoryEnList.size()>0){
                int totalCount= userDepositDormitoryEnList.get(0).getTotalCount();//获取总数
                //计算页数
                if(totalCount%pageSize==0){
                    //计算总页数
                    totalPage= totalCount/pageSize;

                }else {
                    totalPage=totalCount/pageSize+1;
                }
                //设置页数相关
                userQueryResult.setPage(page);
                userQueryResult.setPageTotal(totalPage);
                userQueryResult.setPageSize(pageSize);

                //设置响应数据
                userQueryResult.setStatusCode(JSONStats.QUERY_SUCCESS.getStatusCode());
                userQueryResult.setPageList(userDepositDormitoryEnList);
                userQueryResult.setMessage(JSONStats.QUERY_SUCCESS.getMessage());
            }else {
                //查询失败
                userQueryResult.setStatusCode(JSONStats.QUERY_ERROR.getStatusCode());
                userQueryResult.setMessage(JSONStats.QUERY_ERROR.getMessage());
            }

        }else{
            userQueryResult.setStatusCode(JSONStats.QUERY_ERROR.getStatusCode());
            userQueryResult.setMessage(JSONStats.QUERY_ERROR.getMessage());
        }
        return userQueryResult;
    }
}
